#coding:utf-8

import asyncio, os, json, time
from aiohttp import web
from www.log import logging
from www.models import User
from www.orm import create_pool
from www.web import add_routes
from config.db import db

logger = logging.getLogger(__file__)

# def index(request):
#     return web.Response(body=b'<h1>hello world</h1>', content_type='text/html')

# async def add_user(request):
#     u = User(name='frans')
#     await u.save()
#     return web.Response(body=b'<h1>add user success</h1>', content_type='text/html')

# async def get_user(request):
#     u = await User.find(1)
#     return web.Response(body='<h1>%s</h1>' % u['name'], content_type='text/html')

async def init(loop):
    app = web.Application(loop=loop)
    add_routes(app, 'www.controller')
    srv = await loop.create_server(app.make_handler(), '127.0.0.1', 9009)
    logger.info('server started')
    await create_pool(loop, **db)
    return srv

loop = asyncio.get_event_loop()
loop.run_until_complete(init(loop))
loop.run_forever()